package com.ccn.qrcode.service;

import cn.hutool.core.util.ObjectUtil;
import com.ccn.qrcode.common.Constant;
import com.ccn.qrcode.pojo.QrcodeFilePojo;
import com.ccn.qrcode.utils.QrcodeUtils;
import com.ccn.qrcode.utils.qrcodefile.QrcodeFileUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 文件service
 * @author tig
 */
@Service
public class QrcodeFileService {
    private static final Logger logger = LoggerFactory.getLogger(QrcodeFileService.class);


    @Autowired
    private ThreadPoolExecutor executor;


    /**
     * 文件解析出文本
     * @param multipartFile
     * @return
     */
    public List<QrcodeFilePojo> fileAnalysis(MultipartFile multipartFile) {
        List<QrcodeFilePojo> tmpFile = Lists.newArrayList();
        File file = QrcodeFileUtils.transferToFile(multipartFile);
        if(!file.exists()){
            logger.error("文件不存在,文件全路径[{}]",file.toPath());
            return null;
        }
        InputStreamReader read=null;
        BufferedReader bufferedReader=null;
        try{
            FileInputStream inputStream=new FileInputStream(file);
            read=new InputStreamReader(inputStream,"UTF-8");
            bufferedReader=new BufferedReader(read);
            String lineTxt=null;
            while ((lineTxt=bufferedReader.readLine())!=null){
                QrcodeFilePojo filePojo = QrcodeFileUtils.qrcodeFile(lineTxt);
                if(ObjectUtil.isNotEmpty(filePojo)){
                    tmpFile.add(filePojo);
                }
            }
        }catch (Exception e){
            logger.error("读取文件内容异常：",e);
            return null;
        }finally {
            try{
                if(bufferedReader!=null){
                    bufferedReader.close();
                }
                if(read!=null){
                    read.close();
                }
            }catch (Exception e){
                logger.error("读取文件流关闭异常：",e);
                return null;
            }
        }
        return tmpFile;
    }


    public void createQrcode (QrcodeFilePojo filePojo, String filePath) throws IOException {
        int fileLength = Constant.DEFAULT_LENGTH;
        String fileType = Constant.FORMAT;
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("开始制作二维码");
        //开启第一个异步任务
//        CompletableFuture<Void> createQrcodeFuture = CompletableFuture.runAsync(() -> {
//            //1、远程查询当前要添加商品的信息
//            try {
                QrcodeUtils.createQrcode(filePojo, fileLength,fileType, filePath);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }, executor);
//        CompletableFuture.allOf(createQrcodeFuture).get();
        stopWatch.stop();

    }


}













